package com.edu.boot.controller;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.io.FileUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.edu.boot.common.Base64Utils;
import com.edu.boot.common.ReturnCode;
import com.edu.boot.common.api.CommonResult;
import com.edu.boot.config.UserContext;
import com.edu.boot.entity.*;
import com.edu.boot.entity.Vo.BusinessShopAllInfoVo;
import com.edu.boot.entity.Vo.CommentChildrenCommentReply;
import com.edu.boot.entity.Vo.QyNotesManageVo;
import com.edu.boot.entity.Vo.RecruitmentAndSonNotesManageVo;
import com.edu.boot.enums.OfferStatus;
import com.edu.boot.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Controller
public class BusinessManageController {
    String defaultImgUser = ReturnCode.DEFAULT_IMG_USER;

    @Autowired
    BusinessShopService shopService;
    @Autowired
    private UserService userService;
    @Autowired
    NotesManageService notesManageService;
    @Autowired
    RecuritTypeService recuritTypeService;
    @Autowired
    SResumeService resumeService;
    @Autowired
    CommentService commentService;
    @Autowired
    CommentReplyService commentReplyService;
    /**
     * 企业获取自己的职位申请信息
     */
    @ResponseBody
    @GetMapping("notes/getRecOfferList")
    public CommonResult getRecOfferList(@RequestParam(name = "currentpage") Integer current,
                                        @RequestParam(name = "pagesize") Integer size,
                                        @RequestParam(value = "recName",required = false) String recName,
                                        @RequestParam(value = "offerStatus",required = false) String offerStatus) {
    
        Page<NotesManage> page = new Page<>(current, size);
        // 调用notesManageService获取企业招聘申请列表，包含分页、名称筛选和状态筛选
        IPage<QyNotesManageVo> qyRecOfferList = notesManageService.getQyRecOfferList(page, recName, offerStatus,
                                                                                     UserContext.getUserId(),
                                                                                     UserContext.getPid());
        if (CollectionUtil.isNotEmpty(qyRecOfferList.getRecords())){
            // 调用recuritTypeService获取所有招聘类型映射
            Map<Integer, String> recuritTypeMap = recuritTypeService.getRecuritTypeMap();
            // 调用resumeService获取所有简历信息映射
            Map<Integer, SResume> resumeMap = resumeService.getResumeMap();
            qyRecOfferList.getRecords().forEach(item->{
                item.setRecTypeMsg(recuritTypeMap.get(item.getRecType()));
                item.setOfferStatusMsg(OfferStatus.getByCode(item.getOfferStatus()));
                item.setUserResumeName(Objects.nonNull(resumeMap.get(item.getResumeId())) ?
                                       resumeMap.get(item.getResumeId()).getName() : "");
                item.setPhone(Objects.nonNull(resumeMap.get(item.getResumeId())) ?
                                       resumeMap.get(item.getResumeId()).getPhone() : "");
                item.setEmail(Objects.nonNull(resumeMap.get(item.getResumeId())) ?
                                       resumeMap.get(item.getResumeId()).getEmail() : "");
            });
        }
        return CommonResult.success(qyRecOfferList,"更新简历状态成功！");
    }
    /**
     * 企业邀请求职者面试
     */
    @ResponseBody
    @GetMapping("notes/invite")
    public CommonResult inviteInterview(@RequestParam(value = "nid") Integer nid,
                                        @RequestParam(value = "comment") String comment) {
        // 调用notesManageService根据ID获取面试记录
        NotesManage notesManage = notesManageService.getById(nid);
        if (Objects.isNull(notesManage)){
            return CommonResult.failed("邀请失败！该求职者申请信息不存在");
        }
        notesManage.setComment(comment);
        notesManage.setOfferStatus(OfferStatus.MSZ.getCode());
        // 调用notesManageService更新面试记录
        notesManageService.updateById(notesManage);
        return CommonResult.success();
    }
    /**
     * 企业反馈面试结果
     *
     * API接口信息:
     * - 请求方式: GET
     * - 接口路径: /notes/result
     * - 功能描述: 企业更新求职者的面试结果和状态
     *
     * 请求参数:
     * @param nid 面试记录ID (必填)
     * @param offerStatus 面试结果状态码 (必填)
     * @param result 面试结果详细说明 (必填)
     *
     * 返回结果:
     * @return CommonResult
     * - 成功: 返回成功状态
     * - 失败: 返回失败信息，如"面试结果反馈失败,该求职者申请信息不存在!"
     *
     * 处理流程:
     * 1. 根据nid查询面试记录
     * 2. 验证记录是否存在
     * 3. 更新面试结果和状态
     * 4. 保存更新后的记录
     */
    @ResponseBody
    @GetMapping("notes/result")
    public CommonResult resultInterview(@RequestParam(value = "nid") Integer nid,
                                        @RequestParam(value = "offerStatus") String offerStatus,
                                        @RequestParam(value = "result") String result) {
        // 调用notesManageService根据ID获取面试记录
        NotesManage notesManage = notesManageService.getById(nid);
        if (Objects.isNull(notesManage)){
            return CommonResult.failed("面试结果反馈失败,该求职者申请信息不存在!");
        }
        notesManage.setResult(result);
        notesManage.setOfferStatus(offerStatus);
        // 调用notesManageService更新面试记录
        notesManageService.updateById(notesManage);
        return CommonResult.success();
    }
    

}
